package com.xrz.sxm.aj.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.xrz.sxm.aj.activity.R;
import com.xrz.sxm.aj.entity_tab.Cpmxb;
import com.xrz.sxm.aj.entity_tab.DBHelps;
import com.xrz.sxm.aj.listener.CloseAnimationListener;
import com.xrz.sxm.aj.utils.XUtilBitmapUtil;

import java.util.ArrayList;
import java.util.List;

public class ChanPinView extends LinearLayout {
	/** 计算出ScrollView最大的高度 */
//	private int m_Scrollview_Max_hight;
	private List<Cpmxb> datas = new ArrayList<Cpmxb>();
	private List<ScrollView> scrollviews = new ArrayList<ScrollView>();
	private List<RelativeLayout> RelViews = new ArrayList<RelativeLayout>();
	private List<TextView> TextViews = new ArrayList<TextView>();
	private static final String CLOSE = "close";
	private static final String OPEN = "open";
	private Animation animation_open, animation_close;
	private DBHelps m_DbHelps;

	@SuppressLint("NewApi")
	public ChanPinView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public ChanPinView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ChanPinView(Context context) {
		super(context);
		init();
	}

	private void init() {
		m_DbHelps = new DBHelps(getContext());
		setOrientation(VERTICAL);
		animation_open = AnimationUtils.loadAnimation(getContext(),
				R.anim.scale_open);
		animation_close = AnimationUtils.loadAnimation(getContext(),
				R.anim.scales_close);
		datas.addAll(m_DbHelps.GetChanPinDatas());
		for (int i = 0; i < datas.size(); i++) {
			createView(datas.get(i).cpmc, datas.get(i).tplj, datas
					.get(i).cpjs, datas.size(), i);
		}
	}

	private void createView(String name, String imgpath, String content,
							int len, int pos) {
		RelativeLayout rel = new RelativeLayout(getContext());
		rel.setBackgroundResource(R.mipmap.item_bg);
		TextView text = new TextView(getContext());
		text.setPadding(5, 5, 5, 5);
		text.setText(name);
		text.setTextColor(Color.WHITE);
		text.setTextSize(20);
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		lp.topMargin = 5;
		lp.bottomMargin =5;
		lp.leftMargin = 5;
		lp.addRule(RelativeLayout.CENTER_VERTICAL);
		rel.addView(text, lp);
		TextView text1 = new TextView(getContext());
		text1.setTextColor(Color.WHITE);
		text1.setTextSize(20);
		RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		lp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		lp1.rightMargin = 8;
		lp1.addRule(RelativeLayout.CENTER_VERTICAL);
		rel.addView(text1, lp1);
		addView(rel);
		int w = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		rel.measure(w, h);
		ScrollView scr = new ScrollView(getContext());
//		m_Scrollview_Max_hight = ScreenUtil.getInstant().getDisHight(
//				getContext())
//				- ScreenUtil.getInstant().getTopViewHeight(getContext())
//				- ScreenUtil.getInstant().getTabHeight(getContext())
//				- rel.getMeasuredHeight() * len;
//		LinearLayout.LayoutParams parm = new LinearLayout.LayoutParams(
//				LayoutParams.MATCH_PARENT, m_Scrollview_Max_hight);
//		scr.setLayoutParams(parm);
		LinearLayout m_LinearLayout = new LinearLayout(getContext());
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		m_LinearLayout.setLayoutParams(layoutParams);
		m_LinearLayout.setOrientation(VERTICAL);
		View view=LayoutInflater.from(getContext()).inflate(R.layout.img_chanpin,null);
		ImageView image=(ImageView) view.findViewById(R.id.imageview);
		image.setTag(imgpath);
//		BaseApplication.imageLoader.displayImage(imgpath, image, BaseApplication.options);
		XUtilBitmapUtil.getInstant().displayByWidth(getContext(), image, imgpath);
		m_LinearLayout.addView(view);
		TextView tv = new TextView(getContext());
		tv.setTextSize(20);
		tv.setPadding(5, 5, 5, 5);
		tv.setText("ddddddddddddddddddd");
		Log.e("ddddddddddddddddddd",content);
		m_LinearLayout.addView(tv);
		scr.addView(m_LinearLayout);
//		if (pos == 0) {
//			text1.setText("-");
//			rel.setTag("open");
//		} else {
		text1.setText("+");
		rel.setTag("close");
		scr.setVisibility(View.GONE);
//		}
		scr.setId(Integer.valueOf("1" + pos));
		scr.setTag(image);
		text1.setId(Integer.valueOf("2" + pos));
		rel.setId(pos);
		rel.setOnClickListener(new Click(pos));
		scrollviews.add(scr);
		RelViews.add(rel);
		TextViews.add(text1);
		addView(scr);
	}

	/**
	 * 控制切换
	 *
	 * @param view
	 *            点击的View
	 * @param ContentView
	 *            显示内容的View
	 * @param stateView
	 *            加减号的TextView
	 */
	private void changeState(View view, View ContentView, TextView stateView) {
		String tag = (String) view.getTag();
		if (tag.equals(CLOSE)) {
			ContentView.setVisibility(View.VISIBLE);
			ContentView.startAnimation(animation_open);
			view.setTag(OPEN);
			stateView.setText("-");
			for (RelativeLayout rel : RelViews) {
				if (rel.getId() != view.getId()) {
					rel.setTag(CLOSE);
				}
			}
			for (ScrollView mScrollView : scrollviews) {
				if (mScrollView.getId() != ContentView.getId()) {
					mScrollView.setVisibility(View.GONE);
				}
			}
			for (TextView mTexView : TextViews) {
				if (mTexView.getId() != stateView.getId()) {
					mTexView.setText("+");
				}
			}

		} else {
			ContentView.startAnimation(animation_close);
			animation_close.setAnimationListener(new CloseAnimationListener(
					ContentView));
			view.setTag(CLOSE);
			stateView.setText("+");
		}
	}

	class Click implements OnClickListener {
		int pos;
		public Click(int pos) {
			this.pos = pos;
		}
		@Override
		public void onClick(View v) {
			changeState(v, scrollviews.get(pos), TextViews.get(pos));
		}
	}
}
